package com.book.ch7.knobble_2.group_add_thread_more_level;

/**
 * 线程对象关联线程组：多级关联
 *
 * @Author: xjf
 * @Date: 2019/6/19 10:39
 */
public class Main {

    public static void main(String[] args) {
        //在main组中添加一个线程组A，然后在这个A组中添加线程对象Z
        //方法activeGroup()和activeCount()的值不是固定的
        //是系统中环境的一个快照
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
        ThreadGroup  group = new ThreadGroup(mainGroup,"A");

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("runMethod!");
                    //线程必须在运行状态才可以受组管理
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Thread newThread = new Thread(group,runnable);
        newThread.setName("Z");
        //线程必须启动然后才归到组A中
        newThread.start();

        ThreadGroup[] listGroup = new ThreadGroup[Thread.currentThread().getThreadGroup().activeGroupCount()];
        Thread.currentThread().getThreadGroup().enumerate(listGroup);

        System.out.println("main线程中有多少个子线程组：" + listGroup.length + " 名字为：" +
                listGroup[0].getName() );

        Thread[] listThread = new Thread[listGroup[0].activeCount()];
        listGroup[0].enumerate(listThread);
        System.out.println(listThread[0].getName());
    }
}
